package com.nikmesoft.android.routingbus.controllers.activities;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.nikmesoft.android.routingbus.MyApplication;
import com.nikmesoft.android.routingbus.R;
import com.nikmesoft.android.routingbus.models.PointOverlay;
import com.nikmesoft.android.routingbus.utils.CommonUtil;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;

import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
import android.text.Editable;
import android.widget.TextView;

public class MainActivity extends MapActivity implements LocationListener,
		TextWatcher {
	private MapView mapView;
	private MapController mc;
	private GeoPoint currentPoint;
	private LocationManager lm;
	final Handler mHandler = new Handler();
	private Button btSearch;
	private TextView txtBegin, txtEnd;
	private AutoCompleteTextView editBegin, editEnd;
	String item[] = { "January", "February", "March", "April", "May", "June",
			"July", "August", "September", "October", "November", "December" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	public void onClickAbout(View v) {
		showDialog(0);
	}

	public void onClickHelp(View v) {
		showDialog(1);
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 0:
			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.about)
					.setTitle("About Us")
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									Toast.makeText(getBaseContext(),
											"Enjoy your travel",
											Toast.LENGTH_LONG).show();

								}
							}).setMessage(R.string.about).create();
		case 1:
			return new AlertDialog.Builder(this)
					.setIcon(R.string.help)
					.setTitle("How to use")
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									Toast.makeText(getBaseContext(),
											"Enjoy your travel",
											Toast.LENGTH_LONG).show();

								}
							}).setMessage(R.string.help).create();
		}
		return null;

	}

	// Create runnable for updating
	final Runnable mUpdateResults = new Runnable() {
		public void run() {
			showLocation(currentPoint);
		}
	};

	private void init() {
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		txtBegin = (TextView) findViewById(R.id.TxtBg);
		txtEnd = (TextView) findViewById(R.id.TxtEn);
		editBegin = (AutoCompleteTextView) findViewById(R.id.EdBegin);
		editEnd = (AutoCompleteTextView) findViewById(R.id.EdEnd);
		btSearch = (Button) findViewById(R.id.search);
		btSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				txtBegin.setVisibility(0);
				txtEnd.setVisibility(0);
				editBegin.setVisibility(0);
				editEnd.setVisibility(0);

			}
		});
		editBegin.addTextChangedListener(this);
		editEnd.addTextChangedListener(this);
		editBegin.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, item));
		editEnd.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, item));

		mc = mapView.getController();
		mc.setZoom(16);
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				MyApplication.LOCATION_UPDATE_TIME,
				MyApplication.LOCATION_UPDATE_DISTANCE, this);
		CommonUtil.toastNotify(this, "Waiting for location");
		Thread t = new Thread() {
			public void run() {
				// Get the current location in start-up
				if (lm != null) {
					Location loc = lm
							.getLastKnownLocation(LocationManager.GPS_PROVIDER);
					if (loc != null) {
						currentPoint = new GeoPoint(
								(int) (loc.getLatitude() * 1E6),
								(int) (loc.getLongitude() * 1E6));
						mHandler.post(mUpdateResults);
					}
				}

			}
		};
		t.start();
	}

	private void showLocation(GeoPoint srcGeoPoint) {
		Drawable icon = getResources().getDrawable(R.drawable.marker);
		icon.setBounds(0, 0, icon.getIntrinsicWidth(),
				icon.getIntrinsicHeight());
		PointOverlay overlayItem = new PointOverlay(this, icon);
		OverlayItem item = new OverlayItem(srcGeoPoint, "Location",
				"I'm here (" + srcGeoPoint.getLatitudeE6() / 1E6 + ","
						+ srcGeoPoint.getLongitudeE6() / 1E6 + ")");
		overlayItem.addItem(item);
		mapView.getOverlays().clear();
		mapView.getOverlays().add(overlayItem);
		mc.animateTo(srcGeoPoint);

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (currentPoint != null) {
			mc.animateTo(currentPoint);
		}
	}

	@Override
	protected void onDestroy() {
		lm.removeUpdates(this);
		super.onDestroy();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onLocationChanged(Location location) {
		final Location loc = location;
		if (location != null) {
			CommonUtil.toastNotify(this, "Updating new location");
			Thread t = new Thread() {
				public void run() {
					int lat = (int) (loc.getLatitude() * 1E6);
					int lng = (int) (loc.getLongitude() * 1E6);
					currentPoint = new GeoPoint(lat, lng);
					if (currentPoint != null) {
						mHandler.post(mUpdateResults);

					}
				}
			};
			t.start();

		}
	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

}
